//宫格
// import uniGrid from "/uni_modules/uni-grid/components/uni-grid/uni-grid.vue";
// 公共头部
// import myHeader from "/pages/index/header.vue";
//cache缓存
import Cache from '@/config/cache.js';
//打帮赛方法
// import hitmatchcommon from '@/common/hitmatchcommon.js';
//基础
import methodBase from '@/config/base.js'

export default {
	props: {
		project_make_id: String,
		project_id: String,
	},
	components: {
		// myHeader,
		// uniGrid
	},
	data() {
		return {
			level: 4, //等级数量
			limittime: 16, //限制时间秒
			type: 3, //1=数字  2=字母  3=文字
			letters: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
				"t",
				"u", "v", "w", "x", "y", "z"
			],
			gsc_contents: ["千山鸟飞绝，万径人踪灭。孤舟蓑笠翁，独钓寒江雪。", "白日依山尽，黄河入海流。欲穷千里目，更上一层楼。", "玉阶生白露，夜久侵罗袜。却下水晶帘，玲珑望秋月。",
				"功盖三分国，名成八阵图。江流石不转，遗恨失吞吴。"
			],
			gsc_arr: ["《江雪》", "《登鹳雀楼》", "《玉阶怨》"],
			gsc_name:'',
			question: [], //出题
			shufflequestion: [], //打乱出题
			answer: [], //答案
			proportion: 0, //比例
			ifpass: false, //闯关结果弹出
			defaultdigit: 0, //点击
			selectedindex: 99, //选中特效数字 99没有数字
			passTheResult: ['闯关失败', '闯关成功'],
			fraction: 20, //分值
			btn: ['返回列表'],
			interval: {},
			passtitle: '',
			answertime: 0, //答题时间

		}
	},


	mounted(option = '') {
		var that = this;
		//console.log(that.project_make_id);//赛事
		if (that.project_make_id) {
			Cache.set('project_make_id', that.project_make_id)
		}
		//console.log(that.project_id);//项目
		if (that.project_id) {
			Cache.set('project_id', that.project_id)
		}
		//开始
		that.begin();

		that.interval = setInterval(function() {
			that.limittime = methodBase.updatenumber(that.limittime)
			if (that.limittime <= 0) {
				clearInterval(that.interval);
				//闯关结果
				that.passTheResults(0);
			}
			that.answertime++;
		}, 1000);
	},


	onLoad() {
		console.log('111-111');
		var that = this;
		//开始
		that.begin();
	},
	methods: {
		//开始
		begin() {
			var that = this;


			var question_tmp = [];
			question_tmp = that.question_arr();
			that.shufflequestion = methodBase.shuffle(JSON.parse(JSON.stringify(question_tmp)));
			that.question = question_tmp;

		},
		//出题
		question_arr() {
			var that = this;
			var question_tmp = [];
			if (that.type == 1) {
				for (var i = 1; i <= that.level * that.level; i++) {
					question_tmp.push(i);
				}
			} else if (that.type == 2) {
				for (var i = 0; i < that.level * that.level; i++) {
					question_tmp.push(that.letters[i]);
				}
			} else if (that.type == 3) {
				var gsc_content = that.gsc_contents[0];
				that.gsc_name=that.gsc_arr[0];
				console.log(gsc_content);
				var gsc_arr = gsc_content.split(""); // 将字符串按照每个字符进行拆分
				console.log(gsc_arr);
				that.level = gsc_arr.length /4
				console.log(gsc_arr.length)
				for (var i = 0; i < gsc_arr.length; i++) {
					console.log(i)
					question_tmp.push(gsc_arr[i]);
				}
			}
			that.proportion = parseFloat(100 / that.level); //比例
			return question_tmp;
		},
		passTheResults(state) {
			var that = this;
			console.log(state);
			that.ifpass = true;
			that.passtitle = that.passTheResult[state]
			if (state <= 0) {
				that.fraction = 0;
			}

		},
		//跳转
		clickList(e) {
			var that = this;
			if (e == '0') {
				uni.reLaunch({
					url: '/pages/index/viewtask',
				})
			}
		},

		//点击事件1
		clicknumber(e) {
			var that = this;
			// console.log(e);
			//大于该值
			// console.log(that.question[e]);
			// console.log(that.shufflequestion[that.defaultdigit]);
			// console.log("---------1-----------");
			// return;
			if (that.question[that.defaultdigit] != that.shufflequestion[e]) {
				return;
			}
			var nownumber = that.question[that.defaultdigit]; //当前数字或者文字

			that.answer.push(nownumber); //推入我的答案

			that.clickEffects(e); //点击特效

			that.defaultdigit = ++that.defaultdigit;

			if (that.defaultdigit == that.level * that.level) {
				clearInterval(that.interval);
				//闯关结果
				that.passTheResults(1);
			}
		},
		//点击特效
		clickEffects(number) {
			var that = this;
			that.selectedindex = number
			setTimeout(function() {
				that.selectedindex = 99; //选中特效数字 99没有数字
			}, 200); // 这里的 1000 表示延时的时间，单位是毫秒
		},
	},
	// 在页面的 onUnload 生命周期中停止更新位置
	onUnload() {
		var that = this;
		//清除定时器
		clearInterval(that.interval);
	},
}